package com.lgkj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

import com.lgkj.R;
import com.lgkj.bean.meitao.GoodsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/23 0023.
 */

public class GoodsModelAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<GoodsBean.DataBean.GoodsInfoBean> data;
    private int postion = 0;

    public int getPosition(){
        return postion;
    }
    public GoodsModelAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData(List<GoodsBean.DataBean.GoodsInfoBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public GoodsBean.DataBean.GoodsInfoBean getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_goods_model, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tvUpdata.setChecked((postion == i) ? true : false);
        viewHolder.tvUpdata.setText(data.get(i).getGoods_model());
        viewHolder.tvUpdata.setTag(i);
        viewHolder.tvUpdata.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag().toString());
        if (view.getId() == R.id.tv_updata) {
            callClick.callClick(position);
            this.postion=position;
            notifyDataSetChanged();
        }
    }

    static class ViewHolder {
        @BindView(R.id.tv_updata)
        CheckBox tvUpdata;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private BackCallClick callClick;

    public void setclick(BackCallClick callClick) {
        this.callClick = callClick;
    }

    public interface BackCallClick {
        void callClick(int position);
    }
}
